import re
import time
from flask import request
from flask_restful import abort
from flask_login import current_user, login_user
from funcy import project
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.exc import IntegrityError
from funcy import partial

from redash import models, limiter
from redash.permissions import (
    require_default
)
from redash.handlers.base import (
    BaseResource,
    require_fields,
    get_object_or_404,
    paginate,
    order_results as _order_results,
)

from redash.authentication.account import (
    invite_link_for_user,
    send_invite_email,
    send_password_reset_email,
    send_verify_email,
)
from redash.settings import parse_boolean
from redash import settings

class EmployeeActivityNewResource(BaseResource):
    def get(self):
        result = models.EmployeeActivity.get_id()
        return result[0]

    @require_default
    def post(self):
        req = request.get_json(force=True)
        require_fields(req, ("employeeid","message"))

        employeeactivity = models.EmployeeActivity(
        userid = req["userid"],
        type = req["type"],
        employeeid = req["employeeid"],
        message =req["message"],
        sourcetype = req["sourcetype"],
        )
        try:
            models.db.session.add(employeeactivity)
            models.db.session.commit()
        except IntegrityError as e:
            if "code" in str(e):
                abort(400, message="编号已存在.")
            abort(500)


class EmployeeActivityResource(BaseResource):
    def get(self, employeeactivity_id):
        result = models.EmployeeActivity.get_by_id(employeeactivity_id)
        return result.to_dict()

    @require_default
    def post(self, employeeactivity_id):
        req = request.get_json(force=True)
        require_fields(req, ("employeeid","message"))
            
        employeeactivity = models.EmployeeActivity.get_by_id(employeeactivity_id)
        params = dict(message=req["message"])

        try:
            self.update_model(employeeactivity, params)
            models.db.session.commit()
        except IntegrityError as e:
            if "code" in str(e):
                abort(400, message="编号已存在.")
            abort(500)

class EmployeeActivityDeleteResource(BaseResource):
    @require_default
    def post(self, employeeactivity_id):
        employeeactivity =  models.EmployeeActivity.get_by_id(employeeactivity_id)
        models.db.session.delete(employeeactivity)
        models.db.session.commit()

class EmployeeActivityByEmployeeidResource(BaseResource):
    def get(self, employeeid,sourcetype):
        result = models.EmployeeActivity.get_by_employeeid(employeeid,sourcetype)
        return result[0]
    
class EmployeeActivityListResource(BaseResource):
    def post(self):
        req = request.get_json(force=True)
        page = req["page"]
        page_size = req["pagesize"]
        employeeid = req["employeeid"] if "employeeid" in req else ""
        message = req["message"] if "message" in req else ""
        orderfield = req["orderfield"] if "orderfield" in req else "updated_at"
        order = req["order"] if "order" in req else 1

        

        strwhere ="t1.employeeid=" +str(employeeid)
        if message !="" :
            strwhere = strwhere + " and t1.message like '%"+message+"%'"

        strgetfield =""" t1.*,to_char(t1.updated_at,'YYYY-MM-DD') updated_atformat"""

        tblname = """public.employeeactivity t1"""

        result = models.Enums.get_pagequery(tblname,strgetfield,orderfield,page_size,page,order,strwhere)
        return result[0]

